﻿#include "OpenGLFrameBuffer.h"
#include "OpenGLTextureBuffer.h"
#include "OpenGLRenderBuffer.h"

namespace Kriko
{
    OpenGLFrameBuffer::OpenGLFrameBuffer(size_t width, size_t height)
    {
        glGenFramebuffers(1, &frameBufferID);
        glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID);
        frameTexture = new OpenGLTextureBuffer(width, height);
        renderBuffer = new OpenGLRenderBuffer(width, height);
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
            std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
        this->width = width;
        this->height = height;
        Unbind();
    }

    OpenGLFrameBuffer::~OpenGLFrameBuffer()
    {
        glDeleteFramebuffers(GL_FRAMEBUFFER, &frameBufferID);
        delete frameTexture;
        delete renderBuffer;
    }


    void OpenGLFrameBuffer::Bind()
    {
        glViewport(0, 0, width, height);
        glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID);
    }

    void OpenGLFrameBuffer::Unbind()
    {
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }

    void OpenGLFrameBuffer::BindTexture()
    {
        frameTexture->Bind();
    }

    void OpenGLFrameBuffer::UnbindTexture()
    {
        frameTexture->Unbind();
    }

    int OpenGLFrameBuffer::GetFrameTexture()
    {
        return frameTexture->GetTextureBufferID();
    }
}





